﻿/*
*验证库扩展插件集
*1:url、网址
*2:email、电子邮件
*3:zip、邮政编码
*4:qq、qq号码
*5:phone、电话号码
*6:mobile、手机号
*7:idcard、身份证号码
*8:letter、英文字符
*9:letteru、大写字符
*10:letterl、小写字符
*11:chinese、输入中文
*12:valuesize、带单位的数据:100px、100cm
**/

/*
*扩展规则
*@param {string} value 输入的值
*@param {string} element 要验证的输入对象
*@param {string} 在方法中传入的参数
*@return {boolean} 是否通过验证
txjs.va.rules.* = function(value, element, param)
{
    //验证单选框: if (element.type && element.type == "radio")
    //验证复选框: if (element.type && element.type == "checkbox")
    //验证下拉框: if (element.tagName=="select")
    return {boolean value}; 
};
*/
window.txjs = window.txjs || {};
window.txjs.va = window.txjs.va || {};
window.txjs.va.rules = window.txjs.va.rules || {};

    //trim
    String.prototype.trim = function()
    {
        return this.replace(/(^\s*)|(\s*$)/g, '');
    };
    
//必填项
txjs.va.rules.required = function(value, element, param)
{
    return /.+/.test(value.trim());
};

//url: 网址
txjs.va.rules.url = function(value, element, param)
{
    
    return /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);

};

//email: 电子邮件
txjs.va.rules.email = function(value, element, param)
{
return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
};

//zip: 邮编
txjs.va.rules.zip = function(value, element, param)
{
    return /^\d{6}$/.test(value);
};

//qq号码
txjs.va.rules.qq = function(value, element, param){
    
    return /^[1-9]\d{4,8}$/.test(value);
};

//验证座机
txjs.va.rules.phone = function(value, element, param){
    return /^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/.test(value);
};


//手机号码
txjs.va.rules.mobile = function(value, element, param) {
    return /^(((13[0-9]{1})|(15[0-9]{1})|189)+\d{8})$/.test(value);
};

//身份证号码
txjs.va.rules.idcard = function(value, element, param){ 
var area={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"}  
var value,Y,JYM; 
var S,M; 
var value_array = new Array(); 
value_array = value.split(""); 
//地区检验 
if(area[parseInt(value.substr(0,2))]==null) return false; 
//身份号码位数及格式检验 
switch(value.length){ 
case 15: 
if ( (parseInt(value.substr(6,2))+1900) % 4 == 0 || ((parseInt(value.substr(6,2))+1900) % 100 == 0 && (parseInt(value.substr(6,2))+1900) % 4 == 0 )){ 
ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/;//测试出生日期的合法性 
} else { 
ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/;//测试出生日期的合法性 
} 
if(ereg.test(value)) return true; 
else return false; 
break; 
case 18: 
//18位身份号码检测 
//出生日期的合法性检查  
//闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9])) 
//平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8])) 
if ( parseInt(value.substr(6,4)) % 4 == 0 || (parseInt(value.substr(6,4)) % 100 == 0 && parseInt(value.substr(6,4))%4 == 0 )){ 
ereg=/^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/;//闰年出生日期的合法性正则表达式 
} else { 
ereg=/^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/;//平年出生日期的合法性正则表达式 
} 
if(ereg.test(value)){//测试出生日期的合法性 
//计算校验位 
S = (parseInt(value_array[0]) + parseInt(value_array[10])) * 7 
+ (parseInt(value_array[1]) + parseInt(value_array[11])) * 9 
+ (parseInt(value_array[2]) + parseInt(value_array[12])) * 10 
+ (parseInt(value_array[3]) + parseInt(value_array[13])) * 5 
+ (parseInt(value_array[4]) + parseInt(value_array[14])) * 8 
+ (parseInt(value_array[5]) + parseInt(value_array[15])) * 4 
+ (parseInt(value_array[6]) + parseInt(value_array[16])) * 2 
+ parseInt(value_array[7]) * 1  
+ parseInt(value_array[8]) * 6 
+ parseInt(value_array[9]) * 3 ; 
Y = S % 11; 
M = "F"; 
JYM = "10X98765432"; 
M = JYM.substr(Y,1);//判断校验位 
if(M == value_array[17]) return true; //检测ID的校验位 
else return false; 
} 
else return false; 
break; 
default: 
return false; 
break; 
} 
}; 

//英文字符
txjs.va.rules.letter = function(value, element, param){
    
    return /^[A-Za-z]+$/.test(value);
};

//大写英文
txjs.va.rules.letteru = function(value, element, param){
    
    return /^[A-Z]+$/.test(value);
};

//小写字符
txjs.va.rules.letterl = function(value, element, param){
    
    return /^[a-z]+$/.test(value);
};

//只能输入中文
txjs.va.rules.chinese = function(value, element, param){
    
    return /^[\u4e00-\u9fa5]+$/.test(value);
};

//输入带单位的数据
txjs.va.rules.valuesize = function(value, element, param){
    return /^[0-9]+(%|in|cm|mm|em|ex|pt|pc|px)?$/.test(value);
};









